package com.xuecheng.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.basic.domain.Dictionary;
import com.xuecheng.basic.mappers.DictionaryMapper;
import com.xuecheng.basic.service.DictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.commons.model.vo.DictionaryVo;
import com.xuecheng.commons.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author 七安
 * @since 2022-09-19
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    /**
     * 根据code码查询对应的字典数据
     *
     * @return 字典数据
     */
    @Override
    public DictionaryVo findByCode(String code) {
        //创建wrapper
        LambdaQueryWrapper<Dictionary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dictionary::getCode, code);
        //只取查询到的第一条
        wrapper.last("limit 1");
        //查询记录
        Dictionary dictionary = dictionaryMapper.selectOne(wrapper);
        return BeanHelper.copyProperties(dictionary, DictionaryVo.class);
    }
}
